package yxy.game.pm2.command.altar;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.AltarMessage;
import yxy.game.pm2.module.AltarHandle;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/** 祭坛 武将一键献祭执行 */
@MessageType(proto = Command.AltarHeroOneButtonDo, classtype = AltarMessage.AltarHeroOneButtonDoRequest.class)
public class CMD_AltarHeroOneButtonDo extends Command<AltarMessage.AltarHeroOneButtonDoRequest> {

	@Override
	public void execute(MessageSource messageSource, AltarMessage.AltarHeroOneButtonDoRequest request) {
		String playerCode = messageSource.getTag();

		AltarMessage.AltarHeroOneButtonDoResponse.Builder response = AltarMessage.AltarHeroOneButtonDoResponse.newBuilder();

		HeroHandle heroHandle = new HeroHandle();
		ArrayList<Currency> back = new ArrayList<>();
		Collection<String> heroCodes = heroHandle.getHeroAltarQuick(playerCode);
		//武将献祭前，脱下装备
		try {
			Currencies currencies = unfixEquip(playerCode, heroCodes, back);
			//将装备放回背包
			back.addAll(currencies);
		}catch (Exception e){
			e.printStackTrace();
		}
		//兵法 转换为 魂玉精华
		Currencies hunYu = heroHandle.unfixArtOfWar(playerCode, heroCodes);
		back.addAll(hunYu);

		//魂玉 转 魂玉精华
		Currencies hunYuCurrencies = heroHandle.unfixSoul(playerCode, heroCodes);
		back.addAll(hunYuCurrencies);

		Result result = heroHandle.getHeroAltarBack(playerCode, heroCodes, back);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}

		new CurrencyHandle().addCurrency(playerCode, CCReason.祭坛, back);
		Hero.remove(playerCode, heroCodes, CCReason.祭坛);

		response.addAllBack(new CurrencyHandle().packItems(back));
		response.addAllHeros(new AltarHandle().altarHeros(playerCode));
		send(messageSource, response, Result.Success);

		TaskHandle.handle(playerCode, TaskType.统计型_祭坛_献祭_个数, heroCodes.size());
	}

	/** 献祭前将武将的装备放回背包 */
	private Currencies unfixEquip(String playerCode, Collection<String> heroCodes, ArrayList<Currency> back) throws Exception {
		Currencies temp = new Currencies();
		Iterator<String> iterator = heroCodes.iterator();
		while (iterator.hasNext()){
			String heroCode = iterator.next();
			Hero hero = Hero.get(playerCode, heroCode);
			//获取武将的装备
			int[] equips = hero.getEquips();
			for (int i = 0; i < equips.length; i++) {
				//值为零，该部位没有装备
				if (equips[i] == 0){
					continue;
				}
				Currency currency = Item.get(playerCode, CurrencyType.CT_装备, equips[i]);
				currency.setNumber(1);
				temp.add(currency);
			}
		}
		return temp;
	}

	public void send(MessageSource messageSource, AltarMessage.AltarHeroOneButtonDoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}